package web.filter;

import pojo.User;

import javax.servlet.*;
import java.io.IOException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

//@WebFilter("/lesson12/*")
public class LoginFilter implements javax.servlet.Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        javax.servlet.Filter.super.init(filterConfig);
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req=(HttpServletRequest) servletRequest;
        HttpSession session=req.getSession();
        Object user = session.getAttribute("user");

        //放行
        if (user!=null){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            servletRequest.getRequestDispatcher( "../lesson6/login1.jsp").forward(servletRequest,servletResponse);
        }

    }

    @Override
    public void destroy() {
        javax.servlet.Filter.super.destroy();
    }
}
